package com.ms.p2p.service.impl;

import com.ms.common.exception.NoticeException;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.RedPacketCategoryDao;
import com.ms.p2p.dao.RedPacketDao;
import com.ms.p2p.entity.loan.Loan;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.entity.redpacket.RedPacket;
import com.ms.p2p.entity.redpacket.RedpacketCategory;
import com.ms.p2p.enums.AccountFLow;
import com.ms.p2p.enums.AccountType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chensk on 2017/11/22.
 */
@Service
public class RedPacketServiceImpl    {
    @Autowired
    RedPacketDao readPacketDao;
    @Autowired
    RedPacketCategoryDao redPacketCategoryDao;

    @Autowired
    MemberAccountServiceImpl memberAccountServiceImpl;


    public List findPager(PageBean pageBean, String status, Integer user_id,Integer time_limit){
       return  readPacketDao.findPager(pageBean.getBegin(),pageBean.getPageSize(),status,user_id,time_limit);

    }


    public int findCount() {
        return readPacketDao.findCount();
    }

    public void AwardRedPacket(Member user)  {
        List<HashMap> regcategorysreg = redPacketCategoryDao.findPager(0,null,"reg","1");
        List<HashMap> regcategorysinvites = redPacketCategoryDao.findPager(0,null,"invite","1");
        for(Map cate : regcategorysreg){
            RedPacket  red = new RedPacket();
            red.setMoney(new BigDecimal(cate.get("money").toString()));
            red.setTime_limit(Integer.parseInt(cate.get("time_limit").toString()));
            red.setAddtime(MsDateUtils.getTime());
            red.setTender_limit(new BigDecimal(cate.get("tender_limit").toString()));
            red.setName("注册奖励满"+cate.get("tender_limit")+"抵扣"+cate.get("money"));
            red.setUser_id(user.getId());
            red.setTender_time_limit(Integer.parseInt(cate.get("tender_time_limit").toString()));
            red.setStatus(0);
            red.setOver_time(MsDateUtils.getDayTime(Integer.parseInt(cate.get("time_limit").toString())));
            readPacketDao.insert(red);

        }



    }
    public void MEMBER_ID(Member user)  {

        List<HashMap> regcategorysinvites = redPacketCategoryDao.findPager(0,null,"invite","1");

        if(user.getRecommend_id()!=null&&user.getInvest_times()==0){
            for(Map cate : regcategorysinvites){
                RedPacket  red = new RedPacket();
                red.setMoney(new BigDecimal(cate.get("money").toString()));
                red.setAddtime(MsDateUtils.getTime());
                red.setTime_limit(Integer.parseInt(cate.get("time_limit").toString()));
                red.setTender_limit(new BigDecimal(cate.get("tender_limit").toString()));
                red.setName("邀请奖励满"+cate.get("tender_limit")+"抵扣"+cate.get("money"));
                red.setTender_time_limit(Integer.parseInt(cate.get("tender_time_limit").toString()));
                red.setUser_id(user.getRecommend_id());
                red.setStatus(0);
                red.setOver_time(MsDateUtils.getDayTime(Integer.parseInt(cate.get("time_limit").toString())));
                readPacketDao.insert(red);

            }
        }


    }

    public void useRedPacked(String packet_id, Loan loan, BigDecimal successAmount, String remoteIp) {
       if(StringUtils.isNotBlank(packet_id)){
          RedPacket redPacket = (RedPacket) readPacketDao.get(packet_id);
           if(redPacket.getTender_limit().compareTo(successAmount)>0){
               throw  new NoticeException("投资金额不足以使用该红包");

           }
           if(redPacket.getTender_time_limit()>loan.getTime_limit()){
               throw  new NoticeException("小于红包最低投标月份要求");
           }

           int ret  = readPacketDao.usePacket(packet_id);
           if(ret==0){
                throw  new NoticeException("该红包已经使用或者过期");
           }
           memberAccountServiceImpl.OperAccount(remoteIp,redPacket.getMoney(),redPacket.getUser_id(),"红包返利", AccountType.repackt_award.getValue(), AccountFLow.FLOW_IN.getValue());

       }


    }

    public void overDue() {
        readPacketDao.overDue();
    }
}
